package cn.edu.ldu.protocol;

import cn.edu.ldu.modules.controller.AppUserController;
import cn.edu.ldu.views.modules.controller.UserController;
import com.alibaba.fastjson.JSON;
import cn.edu.ldu.modules.entity.UserEntity;
import cn.edu.ldu.util.Consts;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

public class Register {

    public Register(Protocol protocol){

        UserEntity userEntity = JSON.parseObject(protocol.getData(), UserEntity.class);
        userEntity.setPassword(DigestUtils.sha256Hex(userEntity.getPassword()));

        try {
            Long id = Long.parseLong(AppUserController.setUserInfo(userEntity, null));
            if(!id.equals("")){
                userEntity.setId(id);
                protocol.setData(JSONObject.toJSONString(userEntity));
                protocol.setKey(Consts.NP_REGISTER_SUCCESS);
                protocol.setToken(null);
                UserController.sendDataToUser(protocol);
            }else {
                protocol.setKey(Consts.NP_REGISTER_FAILURE);
//                protocol.setData(JSONObject.toJSONString(userEntity));
                protocol.setToken(null);
                UserController.sendDataToUser(protocol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

}
